package com.yls.except.util;

import com.google.gson.Gson;
import com.yls.except.util.exception.CustomException;
import com.yls.except.util.exception.IndentException;
import com.yls.except.util.exception.PerfectException;
import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 * 返回给app数据
 *
 * @author xusheng
 * @version 1.0
 * @time 2015-7-8 上午9:24:43
 * @Description: {}
 */
public class GeneralResult {
    public static void general(HttpServletResponse resp, List<Object> list, int code, int type) {
        if (type == 1)
            code = getCode(code);
        if (list == null)
            list = new ArrayList<Object>();
        Map<String, Object> oJson = new HashMap<String, Object>();
        oJson.put("code", code);
        String message = getmessage(code, list);
        oJson.put("message", message);
        if (code == 10000) {
            list = new ArrayList<Object>();
        }
        oJson.put("data", list);
        Gson g = new Gson();
        String sJson = g.toJson(oJson);
        writeMessage(resp, sJson);
    }

    public static void generalMap(HttpServletResponse resp, Map<String, Object> map, int code) {
        List<Object> list = new ArrayList<Object>();
        if (map == null || map.size() <= 0)
            general(resp, list, 20013, 0);
        else {
            if (code <= 0)
                general(resp, list, code, 1);
            else {
                Map<String, Object> oJson = new HashMap<String, Object>();
                oJson.put("code", code);
                String message = map.get("message") + "";
                if (StringIsempty.isallempty(message))
                    message = getmessage(20000, list);
                oJson.put("message", message);
                oJson.put("data", map);
                Gson g = new Gson();
                String sJson = g.toJson(oJson);
                writeMessage(resp, sJson);
            }
        }
    }

    public static void generalMap(HttpServletResponse resp, Map<String, Object> map) {
        List<Object> list = new ArrayList<Object>();
        if (map == null || map.size() <= 0)
            general(resp, list, 20013, 0);
        else {
            int code = Integer.parseInt(map.get("tag") + "");
            map.remove("tag");
            String message = map.get("message") + "";
            if (code <= 0)
                map = new HashMap<>();
            code = getCode(code);
            if (StringIsempty.isallempty(message))
                message = getmessage(code, list);
            Map<String, Object> oJson = new HashMap<String, Object>();
            oJson.put("code", code);
            oJson.put("message", message);
            oJson.put("data", map);
            Gson g = new Gson();
            String sJson = g.toJson(oJson);
            writeMessage(resp, sJson);
        }
    }

    public static void writeMessage(HttpServletResponse resp, String sJson) {
        try {
            resp.setCharacterEncoding("UTF-8");
            PrintWriter pw = resp.getWriter();
            pw.print(sJson);
            System.out.println(sJson);
            pw.flush();
            pw.close();
        } catch (IOException e) {
            Logger logger = Logger.getLogger(GeneralResult.class);
            logger.error("e", e);
        }
    }

    public static String getmessage(int code, List<Object> list) {
        String message = "";
        if (code == 10000) {
            if (list != null) {
                if (list.size() > 0) {
                    Exception e = (Exception) (((Map<String, Object>) list.get(0)).get("e"));
                    if (e instanceof MaxUploadSizeExceededException) {
                        long maxSize = ((MaxUploadSizeExceededException) e)
                                .getMaxUploadSize();
                        message = "上传文件太大，不能超过" + maxSize / 1024 / 1024 + "M";
                    } else if (e instanceof CustomException)
                        message = "注册失败";
                    else if (e instanceof PerfectException)
                        message = "请完善资料";
                    else if (e instanceof IndentException)
                        message = "交易失败";
                    else {
                        message = "服务器内部异常";
                    }
                } else {
                    message = "服务器内部异常";
                }
            } else {
                message = "服务器内部异常";
            }
        } else if (code == 10001)
            message = "您还未登录，请先登录";
        else if (code == 10002)
            message = "您的登录已超时，请重新登录";
        else if (code == 10003)
            message = "用户名或密码错误";
        else if (code == 10004)
            message = "改图片类型不支持上传";
        else if (code == 10005)
            message = "改图片过大，不能上传";
        else if(code == 10006)
            message = "请使用微信访问";
        else if (code == 20000)
            message = "";
        else if (code == 20001)
            message = "请完善资料";
        else if (code == 20002)
            message = "未知错误";
        else if (code == 20003)
            message = "获取验证码过于频繁";
        else if (code == 20004)
            message = "该手机号已被注册";
        else if (code == 20005)
            message = "该手机号还未被注册";
        else if (code == 20006)
            message = "该手机号与登录用户绑定手机号不一致";
        else if (code == 20007)
            message = "验证码超时";
        else if (code == 20008)
            message = "验证码错误";
        else if (code == 20009)
            message = "该手机号还未注册";
        else if (code == 20010)
            message = "原绑定手机号不对";
        else if (code == 20011)
            message = "该手机号和获取验证码的手机号不一致";
        else if (code == 20012)
            message = "该用户名已存在";
        else if (code == 20013)
            message = "未找到相关信息";
        else if (code == 20014)
            message = "原密码错误";
        else if (code == 20015)
            message = "原支付密码错误";
        else if (code == 20016)
            message = "您还未设置支付密码，不能操作";
        else if (code == 20017)
            message = "您还未设置支付密码，不能充值";
        else if (code == 20018)
            message = "支付密码错误";
        else if (code == 20019)
            message = "余额不足";
        else if (code == 20020)
            message = "你有认证信息未审核，请勿重复操作";
        else if (code == 20021)
            message = "您的认证信息已通过审核";
        else if (code == 20022)
            message = "您已绑定过银行卡，不能重复绑定";
        else if (code == 20023)
            message = "您还未绑定过银行卡，不能提现";
        else if (code == 20024)
            message = "您还未绑定手机";
        else if (code == 20025)
            message = "获取失败,请联系客服。";
        else if (code == 20026)
            message = "您已绑定了手机号，请勿重复操作";
        else if (code == 20027)
            message = "请输入正确的手机号";
        else if (code == 20028)
            message = "请输入正确格式的验证码";
        else if (code == 20029)
            message = "请输入正确格式的银行卡号";
        else if (code == 20030)
            message = "该优惠券已被使用";
        else if (code == 20031)
            message = "今日已签到";
        else if (code == 20032)
            message = "您已收藏了该店铺";
        else if (code == 20033)
            message = "您还未收藏该店铺";
        else if (code == 20036)
            message = "该店铺已关闭";
        else if (code == 20037)
            message = "使用积分已超过该商品可用积分";
        else if (code == 20038)
            message = "剩余积分不足";
        else if (code == 20039)
            message = "该优惠券不存在";
        else if (code == 20040)
            message = "该优惠券已被使用";
        else if (code == 20041)
            message = "该优惠券已被过期";
        else if (code == 20042)
            message = "该送货地址不存在";
        else if (code == 20043)
            message = "该信息与你无关，不能操作";
        else if (code == 20044)
            message = "该订单信息已处理，请勿重复操作";
        else if (code == 20045)
            message = "该订单已处理，请勿重复操作";
        else if (code == 20046)
            message = "返利金额已超过当天限制金额";
        else if (code == 20047)
            message = "该订单信息有误";
        else if (code == 20048)
            message = "该订单暂不能执行此操作";
        else if (code == 20049)
            message = "积分不足";
        else if (code == 20050)
            message = "该银行卡信息不存在，请重新选择";
        else if (code == 20051)
            message = "可提现金额不足";
        else if (code == 20052)
            message = "可用云点不足";
        else if (code == 20053)
            message = "改订单已评价，请勿重复操作";
        else if (code == 20055)
            message = "您今日已申请过提现，等明日再来吧";
        else if (code == 20056)
            message = "开发中，敬请期待！";
        else if (code == 2057)
            message = "退款失败";
        else if (code == 20058)
            message = "您交易过于频繁，请稍后再试";
        else if (code == 20059)
            message = "请输入数字";
        else if (code == 20060)
            message = "该验证类型不对";
        else if (code == 20061)
            message = "您已点赞过，请勿重复操作";
        else if (code == 20062)
            message = "权限不足";
        else if(code == 20063)
            message = "您已是vip，请勿重复充值";
        else if(code == 20064)
            message = "请输入正确格式的提现金额（最多两位小数，且不能为0）";
        else if(code == 20065)
            message = "您已实名认证，请勿重复操作";
        else
            message = "未知错误";
        return message;
    }

    /**
     * json字符串转为map
     *
     * @param jsonString jons字符串
     * @return
     */
    public static Map<String, Object> jsonStrToMap(String jsonString) {
        if (StringIsempty.isallempty(jsonString))
            return new HashMap<String, Object>();
        JSONObject jsonObject;
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            jsonObject = new JSONObject(jsonString);
            Iterator<?> iterator = jsonObject.keys();
            String key;
            Object value;
            while (iterator.hasNext()) {
                key = (String) iterator.next();
                value = jsonObject.get(key);
                result.put(key, value);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static int getCode(int code) {
        if (code == -1)  //未找到相关信息
            code = 20001;
        else if (code == -2)
            code = 20002;
        else if (code == -3)
            code = 20003;
        else if (code == -4)
            code = 20004;
        else if (code == -5)
            code = 20005;
        else if (code == -6)
            code = 20006;
        else if (code == -7)
            code = 20007;
        else if (code == -8)
            code = 20008;
        else if (code == -9)
            code = 20009;
        else if (code == -10)
            code = 20010;
        else if (code == -11)
            code = 20011;
        else if (code == -12)
            code = 20013;
        else if (code == -13)
            code = 20013;
        else if (code == -14)
            code = 20014;
        else if (code == -15)
            code = 20015;
        else if (code == -16)
            code = 20016;
        else if (code == -17)
            code = 20017;
        else if (code == -18)
            code = 20018;
        else if (code == -19)
            code = 20019;
        else if (code == -20)
            code = 20020;
        else if (code == -21)
            code = 20021;
        else if (code == -22)
            code = 20022;
        else if (code == -23)
            code = 20023;
        else if (code == -24)
            code = 20024;
        else if (code == -25)
            code = 20025;
        else if (code == -26)
            code = 20026;
        else if (code == -27)
            code = 20027;
        else if (code == -28)
            code = 20028;
        else if (code == -29)
            code = 20029;
        else if (code == -30)
            code = 20030;
        else if (code == -31)
            code = 20031;
        else if (code == -32)
            code = 20032;
        else if (code == -33)
            code = 20033;
        else if (code == -34)
            code = 20034;
        else if (code == -35)
            code = 20035;
        else if (code == -36)
            code = 20036;
        else if (code == -37)
            code = 20037;
        else if (code == -38)
            code = 20038;
        else if (code == -39)
            code = 20039;
        else if (code == -40)
            code = 20041;
        else if (code == -42)
            code = 20042;
        else if (code == -43)
            code = 20043;
        else if (code == -44)
            code = 20044;
        else if (code == -45)
            code = 20045;
        else if (code == -46)
            code = 20046;
        else if (code == -47)
            code = 20047;
        else if (code == -48)
            code = 20048;
        else if (code == -49)
            code = 20049;
        else if (code == -50)
            code = 20050;
        else if (code == -51)
            code = 20051;
        else if (code == -52)
            code = 20052;
        else if (code == -53)
            code = 20053;
        else if (code == -54)
            code = 20054;
        else if (code == -55)
            code = 20055;
        else if (code == -56)
            code = 20056;
        else if (code == -57)
            code = 20057;
        else if (code == -58)
            code = 20058;
        else if (code == -59)
            code = 20059;
        else if (code == -60)
            code = 20060;
        else if (code == -61)
            code = 20061;
        else if (code == -62)
            code = 20062;
        else if (code == -63)
            code = 20063;
        else if (code == -64)
            code = 20064;
        else if (code == -65)
            code = 20065;
        else if (code == -10003)
            code = 10003;
        else if (code <= 0)
            code = 20002;
        else
            code = 20000;
        return code;
    }

    public static void resultList(List<Object> list, HttpServletResponse resp) {
        if (list == null || list.size() <= 0)  //判断list是否为空
            general(resp, list, 20013, 0); //未找到相关信息
        else
            general(resp, list, 20000, 0); //成功
    }

    public static void main(String[] args) {
        String ss = "{shopinfo:[{num:5,shopcartid:c6a915d899ed45118c48e29078a1fe3d,shopname:图库,shopid:d227fc0f60fe43e2afcf6712b070d4cf,shopspecid:9cdc55bbd5a448eca3eba10f601418e6,age:1,sex:1,img:httpIMG_AUTHIMG_REGlocalhostIMG_AUTH8081/exceptmanager/data/img/shoppingimg/2016/12/23/330b3fa52c174019a9d6a9f2de7e20d8.jpg,specname:图库,price:20.00}]}";

    }
}